﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using Rafy.Domain;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Rafy.Book.ViewModels
{	
    public class ListingsPageViewModel : BindableBase, INavigationAware
    {
        private bool _isExerciseMode = false;
        /// <summary>
        /// 是否是练习模式
        /// </summary>
        public bool IsExerciseMode
        {
            get => _isExerciseMode;
            set => SetProperty(ref _isExerciseMode, value);
        }

        private bool _isNotMode = true;
        /// <summary>
        /// 是否是非练习模式
        /// </summary>
        public bool IsNotMode
        {
            get => _isNotMode;
            set => SetProperty(ref _isNotMode, value);
        }

        private List<Base_CatalogBook> _catalogs;
        public List<Base_CatalogBook> Catalogs
        {
            get => _catalogs;
            set => SetProperty(ref _catalogs, value);
        }

        private List<Base_Button> _buttons;
        public List<Base_Button> Buttons
        {
            get => _buttons;
            set => SetProperty(ref _buttons, value);
        }

        public DelegateCommand<Base_Button> ButtonCommand { get; private set; }
        public DelegateCommand CancelExerciseCommand { get; private set; }
        public DelegateCommand StartExerciseCommand { get; private set; }
        public ListingsPageViewModel()
        {



            ButtonCommand = new DelegateCommand<Base_Button>(OnButtonClicked);
            CancelExerciseCommand = new DelegateCommand(CancelExercise);
            StartExerciseCommand = new DelegateCommand(StartExercise);
        }
        private void OnButtonClicked(Base_Button btn)
        {
            if (btn.F_Prams == "练习") // 按钮1 触发练习模式
            {
                IsExerciseMode = true;
                IsNotMode = false;
            }
        }

        private void CancelExercise()
        {
            IsExerciseMode = false;
            IsNotMode = true;
        }

        private void StartExercise()
        {
            // 执行开始练习逻辑
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            // 从导航参数中获取工单编号
            var bookId = navigationContext.Parameters.GetValue<int>("bookId");

            if (bookId > 0)
            {
                Console.WriteLine("图书ID=" + bookId);
                LoadCatalogBooks(bookId);
            }
        }

        private void LoadCatalogBooks(int bookId)
        {
            var repo = RF.ResolveInstance<Base_CatalogBookRepository>();
            Catalogs = repo.GetAll().Where(e => e.F_BooksId == bookId).ToList();

            var rp = RF.ResolveInstance<Base_ButtonRepository>();
            var lst = rp.GetsBy(bookId);
            foreach (var item in Catalogs)
            {
                item.Buttons = lst;
            }
            //Console.WriteLine("按钮="+lst.Count);
            //Buttons = lst.ToList();
            //Console.WriteLine("按钮=" + Buttons.Count);

        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // 可以在此处清理或保存数据
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // 如果页面需要重用，则返回 true
            return true;
        }
    }

}
